//
//  CardView.m
//  SmartCheckout
//
//  Created by Barak Edry on 1/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "CardView.h"

@implementation CardView
@synthesize ownerLabel;
@synthesize cardNumLabel;
@synthesize nameLabel;
@synthesize vendorImage;
@synthesize backgroundImage;
@synthesize card;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

    }
    return self;
}

- (void) awakeFromNib {
    [super awakeFromNib];
    card = nil;
    self.layer.opaque = YES;
    self.layer.cornerRadius = 13;
    self.layer.borderWidth = 0;
    self.layer.borderColor = [[UIColor colorWithRed:0.35f green:0.35f blue:0.35f alpha:1.0f] CGColor];
    
    backgroundImage.layer.cornerRadius = 13;
    backgroundImage.clipsToBounds = YES;

    
    cardNumLabel.font = [UIFont fontWithName:@"Halter" size:15];
    
    ownerLabel.font = [UIFont fontWithName:@"Halter" size:11];

    self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    self.layer.shouldRasterize = YES;
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc {
    self.card = nil;
}

- (void) setCard:(Card*)cardToSet {
    card=nil;
    card = cardToSet;
    [self update];
}

- (void) update {   
    
    if (card == nil) {
        return;
    }
    
    //self.nameLabel.text = card.;
    self.ownerLabel.text = card.ownerName;
    self.cardNumLabel.text = card.number;
    self.backgroundImage.backgroundColor = card.backgroundColor;
    
    if ([card.issuer isEqualToString:@"VISA_GOLD"]) {
        self.vendorImage.image = [UIImage imageNamed:@"visa"];
        //self.backgroundImage.backgroundColor = card.backgroundColor;
    } else if ([card.issuer isEqualToString:@"VISA_CLASSIC"]) {
        
        self.vendorImage.image = [UIImage imageNamed:@"visa"];
        //self.backgroundImage.backgroundColor = card.backgroundColor;
    } else if ([card.issuer isEqualToString:@"AMEX"])  {
         self.vendorImage.image = [UIImage imageNamed:@"amex"];
    }
    
    
    
#warning fix this
    //_reflection.image = [self reflectedImage];
    
    
}

+ (CardView*) cardViewWithCard:(Card*)cardToSet {
    CardView* cardView = nil;
    NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CardView" owner:nil options:nil];
    for (id currObject in topLevelObjects) {
        if ([currObject isKindOfClass:[CardView class]]) {
            cardView = (CardView*)currObject;
            break;
        }
    }
    

    cardView.card = cardToSet;
    
    return cardView;
}
@end
